package com.lhz.project.system.controller.base;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.lhz.common.page.BaseQueryParam;
import com.lhz.common.page.ResponseObject;
import com.lhz.framework.aspect.log.annotation.SysLog;
import com.lhz.framework.security.annotation.CheckPermission;
import com.lhz.project.system.model.entity.base.DictData;
import com.lhz.project.system.model.entity.base.DictType;
import com.lhz.project.system.model.param.base.DictDataParam;
import com.lhz.project.system.model.param.base.DictTypeParam;
import com.lhz.project.system.service.base.DictDataService;
import com.lhz.project.system.service.base.DictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiHuaZhi
 * @since 2020-06-22 21:56:13
 */
@Tag(name = "字典管理")
@RestController
@RequestMapping("dict")
@ApiSort(40)
public class DictController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private DictDataService dictDataService;


    @Operation(summary = "查询字典类型", description = "查询字典类型")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = DictType.class))))
    @ApiOperationSupport(order = 10)
    @SysLog(model = "字典管理", name = "查询字典类型")
    @GetMapping
    @CheckPermission(code = "system:dict:query")
    public ResponseObject listDictType(@ParameterObject BaseQueryParam param) {
        Object obj = dictTypeService.listDictType(param);
        return ResponseObject.success(obj);
    }

    @Operation(summary = "查询字典数据", description = "查询字典数据")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = DictData.class))))
    @ApiOperationSupport(order = 11)
    @Parameters({
            @Parameter(name = "dictType", description = "字段类型", in = ParameterIn.PATH, required = true, schema = @Schema(type = "String"))
    })
    @SysLog(model = "字典管理", name = "查询字典数据")
    @GetMapping("/data/{dictType}")
    @CheckPermission(code = "system:dict:query")
    public ResponseObject selectDictDataByType(@PathVariable("dictType") String dictType) {
        Object obj = dictDataService.selectDictDataByType(dictType);
        return ResponseObject.success(obj);
    }

    @Operation(summary = "新增字典类型", description = "新增字典类型")
    @ApiOperationSupport(order = 15)
    @SysLog(model = "字典管理", name = "新增字典类型")
    @PostMapping
    @CheckPermission(code = "system:dict:add")
    public ResponseObject insert(@RequestBody DictTypeParam param) {
        dictTypeService.insert(param);
        return ResponseObject.successPost();
    }

    @Operation(summary = "新增字典数据", description = "新增字典数据")
    @ApiOperationSupport(order = 16)
    @SysLog(model = "字典管理", name = "新增字典数据")
    @PostMapping("/data")
    @CheckPermission(code = "system:dict:add")
    public ResponseObject insert(@RequestBody DictDataParam param) {
        dictDataService.insert(param);
        return ResponseObject.successPost();
    }

    @Operation(summary = "更新字典类型", description = "更新字典类型")
    @ApiOperationSupport(order = 20)
    @SysLog(model = "字典管理", name = "更新字典类型")
    @PutMapping
    @CheckPermission(code = "system:dict:update")
    public ResponseObject updateByPrimaryKey(@RequestBody DictTypeParam param) {
        dictTypeService.updateByPrimaryKey(param);
        return ResponseObject.successUpdate();
    }

    @Operation(summary = "更新字典数据", description = "更新字典数据")
    @ApiOperationSupport(order = 21)
    @SysLog(model = "字典管理", name = "更新字典数据")
    @PutMapping("/data")
    @CheckPermission(code = "system:dict:update")
    public ResponseObject updateByPrimaryKey(@RequestBody DictDataParam param) {
        dictDataService.updateByPrimaryKey(param);
        return ResponseObject.successUpdate();
    }

    @Operation(summary = "删除字典类型", description = "删除字典类型")
    @ApiOperationSupport(order = 25)
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "int"), required = true)
    })
    @SysLog(model = "字典管理", name = "删除字典类型")
    @DeleteMapping("delete/{id}")
    @CheckPermission(code = "system:dict:delete")
    public ResponseObject deleteRealByPrimaryKey(@PathVariable("id") Integer id) {
        dictTypeService.deleteRealByPrimaryKey(id);
        return ResponseObject.successDel();
    }

    @Operation(summary = "删除字典数据", description = "删除字典数据")
    @ApiOperationSupport(order = 26)
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "int"), required = true)
    })
    @SysLog(model = "字典管理", name = "删除字典数据")
    @DeleteMapping("delete/data/{id}")
    @CheckPermission(code = "system:dict:delete")
    public ResponseObject deleteDataRealByPrimaryKey(@PathVariable("id") Integer id) {
        dictDataService.deleteRealByPrimaryKey(id);
        return ResponseObject.successDel();
    }


    @Operation(summary = "批量删除字典数据", description = "批量删除字典数据")
    @ApiOperationSupport(order = 31)
    @SysLog(model = "字典管理", name = "批量删除字典数据")
    @DeleteMapping("delete/batch/data")
    @CheckPermission(code = "system:dict:delete")
    public ResponseObject deleteDataRealByPrimaryKeys(@RequestBody List<Integer> ids) {
        dictDataService.deleteRealByPrimaryKeys(ids);
        return ResponseObject.successDel();
    }
}